package com.leyou.item.web;

import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.PageDTO;
import com.leyou.item.dto.QueryPageBean;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YYP
 * @date 2020/12/28
 */

@RestController
@RequestMapping("/brand")
public class BrandController {

    private final BrandService brandService;
    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }

    /**
     * 根据id查询品牌对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id){
        BrandDTO brandDTO = brandService.queryBrandById(id);
        return ResponseEntity.ok(brandDTO);
    }

    /**
     * 根据品牌id集合查询品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> listBrandByIds(@RequestParam("ids") List<Long> ids) {
        List<BrandDTO> brandDTOList =brandService.listBrandByIds(ids);
        return ResponseEntity.ok(brandDTOList);
    }

    /**
     * 分页查询
     * @param page
     * @param rows
     * @param key
     * @return*/

    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "key",required = false)String key
    ){
        return ResponseEntity.ok(brandService.queryBrandByPage(page,rows,key));
    }

    /*@GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(@RequestBody QueryPageBean queryPageBean){
        PageDTO<BrandDTO> pageDTO = brandService.queryBrandByPage(queryPageBean);
        return ResponseEntity.ok(pageDTO);
    }*/

/*    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> findBrandByPage(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows
    ){
        return ResponseEntity.ok(brandService.queryBrandByPage(page,rows,key));
    }*/

    /**
     * 根据分类id查询品牌
     * @param categoryId
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> listBrandByCategoryId(@RequestParam("id") Long categoryId) {

        List<BrandDTO> brandDTOList = brandService.listBrandByCategoryId(categoryId);
        return ResponseEntity.ok(brandDTOList);
    }

    /**
     * 新增品牌
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand( BrandDTO brandDTO){

        brandService.addBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(null);
    }

    /**
     * 更新品牌
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据id删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id) {
        brandService.deleteBrandById(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
